package com.mayuhao.time.daoImpl;

import com.mayuhao.time.dao.UserDao;
import com.mayuhao.time.util.DBUtil;
import lombok.Data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;

@Data
public class UserDaoImpl implements UserDao {

    //添加数据库信息
    @Override
    public int insUserInfo(String user_ip, String user_alter_time, String user_current_time) {
        Connection con = null;
        PreparedStatement ps = null;
        int i = 0;
        try {
            con = DBUtil.getConnection();
            String sql = "insert  into time_echo.user_info values(?,?,?)";

            ps = con.prepareStatement(sql);

            ps.setString(1, user_ip);
            ps.setString(2, user_alter_time);
            ps.setString(3, user_current_time);

            i = ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(con, ps, null);
        }

        return i;
    }

    //返回查询结果集 封装为list集合
    @Override
    public ArrayList selectIp() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList list = new ArrayList();

        try {
            con = DBUtil.getConnection();

            String sql = "select user_ip from  user_info";
            ps = con.prepareStatement(sql);

            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(rs.getString("user_ip"));

            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(con, ps, rs);
        }

        //集合元素去重
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }

    //判断ip
    @Override
    public void updateUserCount(String user_ip) {

        Connection con = null;
        PreparedStatement ps = null;
        con = DBUtil.getConnection();

        for (int i = 0; i < selectIp().size(); i++) {
            if (user_ip.equals(selectIp().get(i))) {
                System.out.println("匹配");
                //如果ip在数据库中匹配，则访问次数自增
                String sql = "update user_count set user_alter_count=user_alter_count+1 where user_ip = ?";
                try {
                    ps = con.prepareStatement(sql);
                    ps.setString(1, user_ip);
                    ps.executeUpdate();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                return;

            } else {
                System.out.println(selectIp().size());
                System.out.println("新用户");
                //如果ip在数据库中不存在，则新增用户并且新增访问次数
                String sql = "insert  into user_count values(?,?)";
                try {
                    ps = con.prepareStatement(sql);
                    ps.setString(1, user_ip);
                    ps.setInt(2, 1);
                    ps.executeUpdate();
                } catch (SQLException throwables) {
                    System.out.println("增加访问次数出错！");
                }

            }
        }

        if (selectIp().isEmpty()) {
            //如果ip在数据库中不存在，则新增用户并且新增访问次数
            String sql = "insert  into user_count values(?,?)";
            try {
                ps = con.prepareStatement(sql);
                ps.setString(1, user_ip);
                ps.setInt(2, 1);
                ps.executeUpdate();
            } catch (SQLException throwables) {
                System.out.println("存入用户出错！");
            }
        }
        DBUtil.close(con, ps, null);
    }

}